package com.example.uploadhealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.uploadhealth.domain.ResponseResult;
import com.example.uploadhealth.domain.eneity.LoginUser;
import com.example.uploadhealth.domain.eneity.Permission;
import com.example.uploadhealth.domain.vo.PermissionVo;
import com.example.uploadhealth.mapper.PermissionMapper;
import com.example.uploadhealth.service.PermissionService;
import com.example.uploadhealth.utils.BeanCopyUtils;
import com.example.uploadhealth.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (Permission)表服务实现类
 *
 * @author makejava
 * @since 2022-03-22 20:58:54
 */
@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    private List<PermissionVo> inpuList;//输入
    @Override
    public ResponseResult getPermission() {
        System.out.println(1111);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println(loginUser.getUser().getUserName());
        List<Permission> list=new ArrayList<>();
        if(Objects.equals(loginUser.getUser().getUserName(),"zjp")){
            list = list();
        }else {
            LambdaQueryWrapper<Permission> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Permission::getUsertype,"1");
            list = list(queryWrapper);
        }
        inpuList = BeanCopyUtils.copyBeanList(list, PermissionVo.class);
        System.out.println(inpuList);
        //进行递归整理结构的方法
        List<PermissionVo> sort = Sort(inpuList);

        return ResponseResult.okResult(sort);
    }
    public List<PermissionVo> Sort(List<PermissionVo> inpuList) {
        List<PermissionVo> resultList=new ArrayList<>();//输出列表
        for(PermissionVo www:inpuList){
            if(www.getFatherid()==0){//当父级为空时
                resultList.add(www);//当父级为空时，把这一级别的数据放到输出集合里。
                findChildren(resultList);//查询下一级
            }
        }
        return resultList;
    }

    private void findChildren(List<PermissionVo> list) {
        //遍历传进来的子集合（第一次调用则为第一级的数据）
        list.forEach((item)->{
            List<PermissionVo> childrenList =new ArrayList<>();
            //遍历输入列表，查询下一级
            for(PermissionVo www1:inpuList){
                if(Objects.equals(www1.getFatherid(),item.getId())){
                    //如果有下一级，则把数据添加到新创建的子集合中
                    childrenList.add(www1);
                }
            }
            //把子集合添加给当前的数据
            item.setChildren(childrenList);
            //如果子集合为空，说明下一级没有数据了
            if(childrenList.isEmpty()){
                return;
            }
            //对下一级进行遍历，做同样的操作。
            childrenList.forEach((item1)->{
                findChildren(childrenList);
            });
        });
    }
}


